"""
Case Type   : 内核告警
Case Name   : 设置alarm_report_interval参数，测试配置是否生效
Create At   : 2025-10-10
Owner       : lvlintao
Description :
    1、查询enable_alarm参数是否设置为默认值on，
       查询alarm_report_interval是否设置为默认值10s
    2、修改上报间隔参数值为30s，观测是否生效
    3、修改上报间隔参数为非法值，观测是否正常报错
    4、清理环境
Expect      :
    1、显示默认值，on。显示默认值10s;
    2、修改参数成功，参数生效;
    3、修改参数失败，报错提示合理;
    4、清理环境成功
History     :
"""

import os
import unittest
import time

from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

logger = Logger()


class AlarmReportTestCase(unittest.TestCase):
    def setUp(self):
        logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.comsh = CommonSH('PrimaryDbUser')
        self.comm = Common('PrimaryDbUser')
        self.node = Node('PrimaryDbUser')
        status = self.comsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        self.enable_alarm = self.comm.show_param(guc_param='enable_alarm')
        self.alarm_report_interval = self.comm.show_param(guc_param='alarm_report_interval')
        self.set_interval_param = 'alarm_report_interval=30'
        self.show_interval_param = 'show alarm_report_interval;'
        self.err_interval_param = 'alarm_report_interval=abc'

    def test_alarm_report(self):
        text = '--step1:查询enable_alarm，alarm_report_interval是否为默认值;expect:默认为on，10--'
        logger.info(text)
        logger.info(self.enable_alarm)
        self.assertEquals('on', self.enable_alarm, '执行失败' + text)
        logger.info(self.alarm_report_interval)
        self.assertEquals('10', self.alarm_report_interval,
                          '执行失败' + text)

        text = '--step2:设置alarm_report_interval为30s;expect:成功--'
        set_cmd2 = self.comsh.execute_gsguc('reload',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            self.set_interval_param)
        logger.info(set_cmd2)
        show_res = self.comsh.execut_db_sql(self.show_interval_param)
        logger.info(show_res)
        self.assertEquals('30', show_res.splitlines()[-2].strip(),
                          '执行失败' + text)
        dn_name = macro.DN_NODE_NAME.split('/')[0]
        cmd = f'cd {macro.DB_INSTANCE_PATH}/..;mv {dn_name} {dn_name}_1; ls -l'
        logger.info(cmd)
        res = self.node.sh(cmd).result()
        logger.info(res)
        time.sleep(30)

        cmd = f"cd {macro.PG_LOG_PATH}/dn_6001;ls -l | grep 'system_alarm' | awk '{{{{print $NF}}}}'"
        logger.info(cmd)
        file = self.node.sh(cmd).result()
        logger.info(file)
        cmd = f"cd {macro.PG_LOG_PATH}/dn_6001;tail -n 10 {file}"
        res = self.node.sh(cmd).result()
        logger.info(res)
        flag = 'Data or redo directory of data instance dn_6001 does not exist'
        self.assertTrue(res.count(flag) <= 2, '执行失败' + text)
        cur_dn = macro.DB_INSTANCE_PATH.replace(f'{dn_name}', f'{dn_name}_1')
        cmd = f'cd {cur_dn}/..;mv {dn_name}_1 {dn_name}; ls -l'
        logger.info(cmd)
        res = self.node.sh(cmd).result()
        logger.info(res)
        status = self.comsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)

        text = '--step3:修改上报间隔参数为非法值;expect:报错--'
        logger.info(text)
        set_res = self.comsh.execute_gsguc('reload',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           self.err_interval_param,
                                           get_detail=True)
        logger.info(set_res)
        self.assertIn('is incorrect, requires a integer value',
                          set_res.splitlines()[-2].strip(), '执行失败' + text)

    def tearDown(self):
        text = '--step4:清理环境，恢复默认值;expect:成功--'
        logger.info(text)
        set_res = self.comsh.execute_gsguc('reload',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           f"alarm_report_interval=10")
        logger.info(set_res)
        self.assertTrue(set_res, '执行失败' + text)
        cmd = f"source {macro.DB_ENV_PATH};gs_ctl query |grep local_role"
        res = self.node.sh(cmd).result()
        logger.info(res)
        if 'Primary' not in res:
            cmd = f"source {macro.DB_ENV_PATH};gs_ctl switchover;"
            res = self.node.sh(cmd).result()
            logger.info(res)
            cmd = f"source {macro.DB_ENV_PATH};gs_ctl query |grep local_role"
            res = self.node.sh(cmd).result()
            logger.info(res)
        self.assertIn('Primary', res, '执行失败' + text)
        logger.info(f'-----{os.path.basename(__file__)} end-----')
